package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.infos.ClubDataModel;
import com.badibadi.infos.Record_Info_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.ImageTools;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.SelectPicLayoutActivity;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.ShowErWeiMaActivity;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 俱乐部资料 (管理员的)2014、10、11
 * 
 * @author Administrator
 * 
 */
public class ClubDataGLYActivity extends BaseActivity implements
		OnClickListener {

	protected ViewHolder holder;
	private ClubDataModel dataModel;// 数据存放处
	private DisplayImageOptions options;
	protected static final String TAG = "ClubDataGLYActivity";
	private static final int ErWeiMaZD = 9000;
	private String cid = "11";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubDataGLYActivity.this);
				try {
					Utils.showMessage(ClubDataGLYActivity.this, getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:
				Utils.ExitPrgress(ClubDataGLYActivity.this);
				// initListView();
				try {
					initView();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(ClubDataGLYActivity.this);
				try {
					Utils.showMessage(ClubDataGLYActivity.this, getResources()
							.getString(R.string.l_xb49));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_club_data);

		this.findViewById(R.id.my_data_return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		cid = getIntent().getStringExtra("cid");
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
		holder = new ViewHolder(ClubDataGLYActivity.this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		abum(cid);
	}

	private void initView() {
		// TODO Auto-generated method stub
		try {
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl + "/" + dataModel.getLogo()
							+ Constants.appPhoto4img, holder.logo, options);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl + "/" + dataModel.getRoom_head()
							+ Constants.Appactivitycover, holder.room_head,
					options);
		} catch (Exception e) {
			// TODO: handle exception
		}
		holder.admin.setText("" + dataModel.getAdmin());
		holder.info.setText("" + dataModel.getInfo());
		holder.createtime.setText(""
				+ Utils.Get_Data(dataModel.getCreatetime(), "yyyy-MM-dd HH:mm",
						"000"));
		holder.originate_address.setText("" + dataModel.getOriginate_address());
		holder.suit_persons.setText("" + dataModel.getSuit_persons());
		holder.limitnum.setText("" + dataModel.getLimitnum());
		holder.price.setText("" + dataModel.getPrice());
		holder.typeName.setText("" + dataModel.getTypeName());
		holder.erweima.setImageBitmap(Utils.getClubErWeiMa(cid,
				DisplayUtil.dip2px(this, 80), DisplayUtil.dip2px(this, 80)));

		initClickListener();
	}

	protected void initClickListener() {
		holder.r0.setOnClickListener(ClubDataGLYActivity.this);
		holder.r1.setOnClickListener(ClubDataGLYActivity.this);
		holder.r2.setOnClickListener(ClubDataGLYActivity.this);
		holder.r3.setOnClickListener(ClubDataGLYActivity.this);
		holder.r4.setOnClickListener(ClubDataGLYActivity.this);
		holder.r5.setOnClickListener(ClubDataGLYActivity.this);
		holder.r6.setOnClickListener(ClubDataGLYActivity.this);
		holder.r7.setOnClickListener(ClubDataGLYActivity.this);
		holder.r8.setOnClickListener(ClubDataGLYActivity.this);
		holder.r9.setOnClickListener(ClubDataGLYActivity.this);
		holder.r10.setOnClickListener(ClubDataGLYActivity.this);
	}

	public class ViewHolder {
		RelativeLayout r0, r1, r3, r4, r6, r7, r8, r9, r10;
		LinearLayout r2, r5;

		// 代表图
		ImageView logo, room_head, erweima;
		TextView admin, info, createtime, originate_address, suit_persons,
				limitnum, price, typeName;

		public ViewHolder(Context context) {
			logo = (ImageView) ((Activity) (context)).findViewById(R.id.logo);
			room_head = (ImageView) ((Activity) (context))
					.findViewById(R.id.room_head);
			erweima = (ImageView) ((Activity) (context))
					.findViewById(R.id.erweima);

			admin = (TextView) ((Activity) (context)).findViewById(R.id.admin);
			info = (TextView) ((Activity) (context)).findViewById(R.id.info);
			createtime = (TextView) ((Activity) (context))
					.findViewById(R.id.createtime);
			originate_address = (TextView) ((Activity) (context))
					.findViewById(R.id.originate_address);
			suit_persons = (TextView) ((Activity) (context))
					.findViewById(R.id.suit_persons);
			limitnum = (TextView) ((Activity) (context))
					.findViewById(R.id.limitnum);
			price = (TextView) ((Activity) (context)).findViewById(R.id.price);
			typeName = (TextView) ((Activity) (context))
					.findViewById(R.id.typeName);

			r0 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL0);
			r1 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL1);
			r2 = (LinearLayout) ((Activity) (context)).findViewById(R.id.RL2);
			r3 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL3);
			r4 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL4);
			r5 = (LinearLayout) ((Activity) (context)).findViewById(R.id.RL5);
			r6 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL6);
			r7 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL7);
			r8 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL8);
			r9 = (RelativeLayout) ((Activity) (context)).findViewById(R.id.RL9);
			r10 = (RelativeLayout) ((Activity) (context))
					.findViewById(R.id.RL10);
		}

	}

	/**
	 * 俱乐部资料页面---POST
	 * 
	 * @param $cid
	 *            //俱乐部ID
	 * @param $languageType
	 *            ;（必需）//name为中文简体，tw_name 中文繁体 en_name 英文
	 */
	protected void abum(final String cid) {
		Utils.showPrgress(ClubDataGLYActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cid", cid);
				map.put("languageType", Dialog
						.getSystemLanguageTypegrzx(ClubDataGLYActivity.this));
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/abum");
//				   
				if (content != null) {
					Results results = Utils.checkResult_NNN(
							ClubDataGLYActivity.this, content);
//					   
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							dataModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), ClubDataModel.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {

					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.RL0:// 代表图
			// Intent intent0 = new Intent(ClubDataGLYActivity.this,
			// SelectPicLayoutActivity.class);
			// startActivityForResult(intent0, 0);
			break;
		case R.id.RL1:// 管理员
			// Intent intent1 =new
			// Intent(ClubDataGLYActivity.this,ModifyTheClubDataActivity.class);
			// startActivityForResult(intent1, 1);
			break;
		case R.id.RL2:// 俱乐部宗旨
			Intent intent2 = new Intent(ClubDataGLYActivity.this,
					ModifyTheClubDataActivity.class);
			intent2.putExtra("zongzhi", dataModel);
			// startActivityForResult(intent2, 2);
			startActivity(intent2);
			break;
		case R.id.RL3:// 创立时间
			// Intent intent3 = new Intent(ClubDataGLYActivity.this,
			// ModifyTheClubDataActivity.class);
			// intent3.putExtra("xiugai", "" + dataModel.getAdmin());
			// startActivityForResult(intent3, 3);
			break;
		case R.id.RL4:// 发源地
			// Intent intent4 = new Intent(ClubDataGLYActivity.this,
			// ModifyTheClubDataActivity.class);
			// intent4.putExtra("xiugai", "" + dataModel.getAdmin());
			// startActivityForResult(intent4, 4);
			break;
		case R.id.RL5:// 适合族群
			Intent intent5 = new Intent(ClubDataGLYActivity.this,
					ModifyTheClubDataActivity.class);
			intent5.putExtra("zhuqun", dataModel);
			// startActivityForResult(intent5, 5);
			startActivity(intent5);
			break;
		case R.id.RL6:// 人数上限
			Intent intent6 = new Intent(ClubDataGLYActivity.this,
					ModifyTheClubDataActivity.class);
			intent6.putExtra("renshu", dataModel);
			// startActivityForResult(intent6, 6);
			startActivity(intent6);
			break;
		case R.id.RL7:// 参加俱乐部所需金币
			Intent intent7 = new Intent(ClubDataGLYActivity.this,
					ModifyTheClubDataActivity.class);
			intent7.putExtra("jinbi", dataModel);
			// startActivityForResult(intent7, 7);
			startActivity(intent7);
			break;
		case R.id.RL8:// 俱乐部类别(不能修改)
			// Intent intent8 =new
			// Intent(ClubDataGLYActivity.this,ModifyTheClubDataActivity.class);
			// startActivityForResult(intent8, 8);
			break;
		case R.id.RL9:// 俱乐部背景
			// Intent intent9 = new Intent(ClubDataGLYActivity.this,
			// SelectPicLayoutActivity.class);
			// startActivityForResult(intent9, 9);
			break;
		case R.id.RL10:// 俱乐部二维码
			Intent intent = new Intent(ClubDataGLYActivity.this,
					ShowErWeiMaActivity.class);
			intent.putExtra("erweima", Utils.getClubErWeiMa(dataModel.getId()));
			startActivityForResult(intent, ErWeiMaZD);

			// Intent intent = new Intent(ClubDataGLYActivity.this,
			// ShowErWeiMaActivity.class);
			// intent.putExtra("erweima", Utils.getClubErWeiMa(cid));
			// startActivityForResult(intent, ErWeiMaZD);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

		super.onActivityResult(requestCode, resultCode, data);

	}
}
